package com.hspedu.socket;


import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/3/2
 * @time 17:32
 */
public class SocketTCP04Client {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket(InetAddress.getLocalHost(),9999);
        OutputStream outputStream = socket.getOutputStream();
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
        BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
        bufferedWriter.write("hello,server 字符流");
        bufferedWriter.newLine();
        bufferedWriter.flush();

        InputStream inputStream = socket.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        try {
            String line;
            line = bufferedReader.readLine();
                // ... do something with line
                System.out.println(line);

        } catch (IOException e) {
            // ... handle IO exception
            e.printStackTrace();
        }

        bufferedReader.close();
        bufferedWriter.close();
        socket.close();
    }
}
